var app = getApp();

var host = "http://api.xinqing.com";
// var host = "https://xq.elenote.com";

/**
 * POST请求，
 * URL：接口
 * postData：参数，json类型
 * doSuccess：成功的回调函数
 * doFail：失败的回调函数
 */
function postRequest(url, postData, doSuccess) {
  wx.request({
    //项目的真正接口，通过字符串拼接方式实现
    url: host + url,
    header: {
      "content-type": "application/json;charset=UTF-8"
    },
    data: postData,
    method: 'POST',
    success: function (res) {
      //参数值为res.data,直接将返回的数据传入
      console.log(res)
      doSuccess(res.data);
    },
    fail: function () {
      console.log(info);
    },
  })
}

//GET请求，不需传参，直接URL调用，
function getRequest(url, doSuccess) {
  wx.request({
    url: host + url,
    header: {
      "content-type": "application/json;charset=UTF-8"
    },
    method: 'GET',
    success: function (res) {
      doSuccess(res.data);
    },
    fail: function (info) {
      console.log(info);
    },
  })
}

module.exports = {
  postRequest: postRequest,
  getRequest: getRequest,
  host: host
}
